package com.taiyuan.zfmvp.redis.controller;

import com.alibaba.fastjson.JSON;
import com.taiyuan.zfmvp.common.annotation.MyAnnotation;
import com.taiyuan.zfmvp.redis.model.User;
import com.taiyuan.zfmvp.redis.service.RedisService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @param request
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption:
 * @Date 2022/2/6
 * @return
 */
@RequestMapping("/id")
@RestController
public class RedisController {

    @Resource
    private RedisService redisService;
    @Resource
    private RedisTemplate redisTemplate;
    /**
     * @Author zhoufang@si-tech.com.cn
     * @Description  test-redis
     * @Date 2022/2/6
     * @Param
     * @Return
     */
    @PostMapping("/getByKey")
    @ResponseBody
    public String testRedis(@RequestBody Map<String, Object> inMap){
        try {
            String key = inMap.get("a")+"";
            Object keyValue = redisService.get(key);
            return keyValue.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * @Author zhoufang@si-tech.com.cn
     * @Description  hash put
     * @Date 2022/2/20
     * @Param
     * @Return
     */
    @PostMapping("/putHash")
    @ResponseBody
    public String putHash(@RequestBody Map<String, String> inMap){
        try {
            redisService.hmSetObject(inMap.get("id")+"user",inMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }



    /**
     * @Author zhoufang@si-tech.com.cn
     * @Description  hash get
     * @Date 2022/2/20
     * @Param
     * @Return
     */
    @PostMapping("/getHash")
    @ResponseBody
    public String getHash(@RequestBody Map<String, Object> inMap){
        try {
            Map<Object,Object> user = redisService.hmGetObject(inMap.get("id")+"user");
            User us = new User();
            us = JSON.parseObject(JSON.toJSONString(user), User.class);
            return us.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * @Author zhoufang@si-tech.com.cn
     * @Description  分布式锁测试
     * @Date 2022/11/19
     * @Param
     * @Return
     */
    @PostMapping("/distributedTest")
    @ResponseBody
    @MyAnnotation
    public String distributedTest(@RequestBody Map<String, Object> inMap){
        //lock+商品id
        String key = "lock"+inMap.get("commodityId");
        //value
        String value = inMap.get("orderId")+"";
        try {
            //1. 获取分布式锁
            Boolean flag = redisService.getDistributedLock(key,value,10l);

            //2.执行具体操作
            if(flag){
                System.out.println("+++++++++++ "+inMap.get("commodityId")+" ++++++++++++++++++");
                System.out.println("+++++++++++ "+inMap.get("orderId")+" ++++++++++++++++++");
            }
            //
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //3.释放锁
            redisService.delDistributedLock(key);
        }
        return "";
    }


    /**
     * @Author zhoufang@si-tech.com.cn
     * @Description  test-redis
     * @Date 2022/2/6
     * @Param
     * @Return
     */
    @GetMapping("/getByKey/{key}")
    @ResponseBody
    public String testRedis(@PathVariable("key") String key){
        try {
            String keyValue = redisService.get(key).toString();
            return keyValue;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
